#ifndef _HEAD_H
#define _HEAD_H
#include <my_head.h>
#endif

#ifndef _UTILS_H
#define _UTILS_H
#include"./clientHeadFile/utils.h"
#endif
char my_get()
{
    char buf[20];
    read(STDIN_FILENO , buf , 20);
    return buf[0];
}

bool addEvent(int con_fd , int epo_fd)
{
    epoll_event event;
    event.data.fd = con_fd;
    event.events = EPOLLIN;
    epoll_ctl(epo_fd , EPOLL_CTL_ADD , con_fd , &event);
    return true;
}
void easyGui()
{
    printf("----------------\n");
    printf("----登录按1-----\n");
    printf("----注册按2-----\n");
    printf("----退出按3-----\n");
    printf("----------------\n");
}
void easyGui2()
{
    printf("----------------\n");
    printf("----文件按1-----\n");
    printf("----聊天按2-----\n");
    printf("---返回上级按3--\n");
    printf("----------------\n");
}
int clientConnect(char *sourceIP , char *sourcePort)
{
    int socketFd = socket(AF_INET, SOCK_STREAM, 0);
    // 方式一: inet_addr
    // 把 点分十进制, 转成in_addr_t类型(网络IP),  把其存储到结构体in_a   11    //in_addr_t addrTIP = inet_addr(sourceIP);
    //struct in_addr inAddr;
    //inAddr.s_addr = addrTIP;
   
    // 方式二: inet_aton
    struct in_addr inAddr;
    inet_aton(sourceIP, &inAddr);
    // 把端口转为int类型
    int sourcePortInt = atoi(sourcePort);
    // 把端口号: 有主机字节序, 转为网络字节序
    int sourcePortNet = htons(sourcePortInt);

    // 构建"struct sockaddr"类型
    struct sockaddr_in socketAddr;
    socketAddr.sin_family = AF_INET;
    socketAddr.sin_addr = inAddr;
    socketAddr.sin_port = sourcePortNet;

    // 客户端向服务器发起建立连接请求
    int res_connect = connect(socketFd, (struct sockaddr *)&socketAddr , sizeof(socketAddr));
    CHECK_ERROR(res_connect , -1 , "connect has occured fail!\n");

    return socketFd;
}

//内部方法跳过输入前面的字符 , 返回第一个不是空格的位置
static int jumpSpace(int i  , char * a)
{
    while(a[i] == ' ' && i < strlen(a))i++;
    if(i >= strlen(a))return -1;
    return i;
}


//获取文件名字
static void getFileName(int j , char * source , char * dest)
{
    int i = 0;
    while(j < strlen(source) && source[j]!=' ')
    {
        dest[i] = source[j];
        i++;
        j++;
    }
    dest[i] = '\0';
}

//处理消息
bool processMes(userMes * usermes , char * buf)
{
    usermes->mesType = CONTROL;
    usermes->contentSize = 0;
    memset(usermes->content , 0 , MAX_BUF_FILE);
    

    //跳过前面的空白字符
    int i = 0;
    i = jumpSpace(i , buf);
    if(i == -1 )return false; //全是空白字符， 直接返回错误
    char typeBuf[20];//命令字符串
    //给命令字符串赋值
    int j = 0;
    for( ; i < strlen(buf) && buf[i]!=' ' ; i++ , j++)
    {
        typeBuf[j] = buf[i];
    }
    typeBuf[j] = '\0';
    j++;


    j = jumpSpace(j , buf);
    if(j == -1)return false;
    //printf("utils.c /100:字符串是%s,大小是%ld\n" , typeBuf, strlen(typeBuf));
    
    if(strcmp("cd" , typeBuf) == 0)
    {
        usermes->cType = cd;
        j = jumpSpace(j , buf);
        getFileName(j , buf , usermes->content);
        usermes->contentSize = strlen(usermes->content) - 1;
    }
    else if(strcmp("ls" , typeBuf) == 0)
    {
        usermes->cType = ls;
        j = jumpSpace(j , buf);
        getFileName(j , buf , usermes->content);
        usermes->contentSize = strlen(usermes->content) - 1;
    }
    else if(strcmp("pwd" , typeBuf) == 0)
    {
        usermes->cType = pwd;
    }
    else if(strcmp("gets" , typeBuf) == 0)
    {
        usermes->cType = _gets;
        j = jumpSpace(j , buf);
        getFileName(j , buf , usermes->content);
        usermes->contentSize = strlen(usermes->content) - 1;
    }
    else if(strcmp("puts" , typeBuf) == 0)
    {
        usermes->cType = _puts;
        j = jumpSpace(j , buf);
        getFileName(j , buf , usermes->content);
        usermes->contentSize = strlen(usermes->content) - 1;
    }
    else if(strcmp("remove" , typeBuf) == 0)
    {
        usermes->cType = _remove;
        j = jumpSpace(j , buf);
        getFileName(j , buf , usermes->content);
        usermes->contentSize = strlen(usermes->content) - 1;
    }
    else
    {
        printf("没有此命令\n");
        return false;
    }
    return true;
}

//处理返回消息的具体逻辑
bool processRecvMes(int sofd , User * me)
{
    void what_is_YES(recvMes *, User *);
    recvMes recvmes;
    memset(&recvmes , 0 , sizeof(recvMes));
    int recv_flag = recv(sofd , &recvmes , sizeof(recvMes) - MAX_BUF_FILE , 0);
    if(recv_flag == -1)return false;
    else recv_flag += recv(sofd, recvmes.content , recvmes.contentSize , 0);

    switch(recvmes.rStat)
    {
    case YES:
        what_is_YES(&recvmes , me);
        break;
    case NO:
        printf("utils.c/ 180:处理recvType: %d 失败\n");
        return false;
    case ERROR:
        printf("utils.c/ 183:服务器异常\n");
        return false;
    }

    return true;
}
void what_is_YES(recvMes * recvmes , User * me)
{
    switch(recvmes->cType)
    {
    case cd:
        printf("currpath is %s \n return path is %s\n" , me->currPath , recvmes->content);
        break;
    case ls:
        break;
    case _puts:
        break;
    case _gets:
        break;
    case pwd:
        break;
    case noControl:
        break;
    case _remove:
        break;
    }
}
